package com.hzjy.common.utils;

import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.hzjy.common.config.RuoYiConfig;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

import java.io.*;
import java.util.Map;

public class WordUtils {

    public static String fillWord(Map<String, Object> dataMap, String path, String templateName) {
        try {

            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");

            /**
             * 以下是两种指定ftl文件所在目录路径的方式，注意这两种方式都是
             * 指定ftl文件所在目录的路径，而不是ftl文件的路径
             */
            //指定路径的第一种方式（根据某个类的相对路径指定）
//                configuration.setClassForTemplateLoading(this.getClass(), "");

            //指定路径的第二种方式，我的路径是C：/a.ftl
            configuration.setDirectoryForTemplateLoading(new File(path));
            String filePath = RuoYiConfig.getUploadPath() + "/xieyi/test.doc";
            //输出文档路径及名称
            File outFile = new File(filePath);
            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate(templateName, "utf-8");
            FileOutputStream fileOutputStream = new FileOutputStream(outFile);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
            Writer out = new BufferedWriter(outputStreamWriter, 10240);
            template.process(dataMap, out);
            out.close();
            outputStreamWriter.close();
            fileOutputStream.close();
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void copyFileUsingFileStreams(File source, File dest)
            throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }

    public static void word2pdf(String docPath, String savePath) throws Exception {
        String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
        ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
        License license = new License();
        license.setLicense(is);
        com.aspose.words.Document document = new com.aspose.words.Document(docPath);
        document.save(new FileOutputStream(new File(savePath)), SaveFormat.PDF);
    }

    public static void main(String[] args) {
        //加载测试文档
//        word2pdf("d:/test/222/test.doc", "d:/test/222/333.pdf");
        //将文档指定页保存为Png格式的图片
//        BufferedImage image = doc.saveToImages( 0, ImageType.Bitmap);
//        File file = new File("ToPNG.png");
//        ImageIO.write(image, "PNG", file);

        //将Word转为PDF
//        doc.saveToFile("Word转PDF.pdf", FileFormat.PDF);

//        //将Word保存为SVG格式
//        doc.saveToFile("ToSVG.svg",FileFormat.SVG);
//
//        //将Word保存为RTF格式
//        doc.saveToFile("ToRTF.rtf",FileFormat.Rtf);
//
//        //将Word保存为XPS格式
//        doc.saveToFile("ToXPS.xps",FileFormat.XPS);
//
//        //将Word保存为XML格式
//        doc.saveToFile("ToXML.xml",FileFormat.Xml);
//
//        //将Word保存为TXT格式
//        doc.saveToFile("ToTXT.txt",FileFormat.Txt);
    }

}
